package aorise.com.frame.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hjq.toast.ToastUtils;

import aorise.com.frame.R;
import aorise.com.frame.common.LogT;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：zhangliwen Administrator on 2018/7/30 14:47
 * 邮箱：1871907207@qq.com
 */
public class BaseFragment extends Fragment {
    private static final String TAG = BaseFragment.class.getSimpleName();
    private ActivityActionBarClick onActionBarItemClick;
    private Toast mToast = null;
    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {


        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public void showToast(String text) {
        if (this.mToast == null) {
            this.mToast = Toast.makeText(getActivity(), text, Toast.LENGTH_LONG);
        } else {
            this.mToast.setText(text);
        }

        this.mToast.show();
    }

    //rxjava线程转换
    public Observable.Transformer schedulersTransformer() {
        return new Observable.Transformer() {
            @Override
            public Object call(Object observable) {
                return ((Observable) observable).subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public void showToast(@StringRes int resId) {
        this.showToast(this.getString(resId));
    }
    public void openActivity(Class<?> pClass) {
        this.openActivity(pClass, (Bundle)null);
    }

    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(getContext(), pClass);
        if(pBundle != null) {
            intent.putExtras(pBundle);
        }
        this.startActivity(intent);
    }

    public void initActionBar(View view,ActivityActionBarClick onclick){
        this.view = view;
        this.onActionBarItemClick = onclick;
    }
    public ImageView getActionbarBackView(Activity activity){
        if(view == null){
            ToastUtils.show("请先执行initActionBar(actionbarView,ActivityActionBarClick）");
            activity.finish();
        }
        ImageView mImageView = view.findViewById(R.id.title_back);
        mImageView.setVisibility(View.VISIBLE);
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onActionBarItemClick != null) {
                    onActionBarItemClick.onBackClick();
                }else{
                    LogT.d("请设置actionbar 返回键的点击事件");
                }
            }
        });
        return mImageView;
    }
    public TextView getActionbarTitleView(Activity activity){
        if(view == null){
            ToastUtils.show("请先执行initActionBar(actionbarView,ActivityActionBarClick）");
            activity.finish();
        }
        TextView mTextView = view.findViewById(R.id.title_name);
        return mTextView;
    }
    public ImageView getActionbarMenuView(Activity activity){
        if(view == null){
            ToastUtils.show("请先执行initActionBar(actionbarView,ActivityActionBarClick）");
            activity.finish();
        }
        ImageView mImageView = view.findViewById(R.id.title_menu);
        View menuView = view.findViewById(R.id.title_menu_linear);
        mImageView.setVisibility(View.VISIBLE);
        menuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onActionBarItemClick !=null){
                    onActionBarItemClick.onMenuClick();
                }else {
                    LogT.d("请设置actionbar 菜单键的点击事件");
                }
            }
        });
        return mImageView;
    }
    private void destroyToast() {
        if (this.mToast != null) {
            this.mToast.cancel();
            this.mToast = null;
        }

    }
}
